// pages/receiveing-adress/receiveing-adress.js
const util = require('../../utils/util.js');
import api from '../../utils/request/api'
import {
	addressList
} from '../../utils/request/apiurl'



Page({
	data: {
		// adressList:[
		// 	{
		// 		id:0,
		// 		name:'郑富红',
		// 		tel:'156****1234',
		// 		defaultAdress:true,
		// 		adressDetail:'重庆市南岸区海棠溪街道国瑞大厦15楼重庆市南岸区海棠区',	
		// 	},
		// 	{
		// 		id:1,
		// 		name:'郑红',
		// 		tel:'156****1234',
		// 		defaultAdress:false,
		// 		adressDetail:'重庆市南岸区海棠溪街道国瑞大厦15楼重庆市南岸区海棠区',	
		// 	}
		// ],
		adressList: [],
		uid: '',
		adressId: '',
		id:'',//购物车id
		addressInfor:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var userInfo = wx.getStorageSync('userInfo');
		// console.log(options)
		// debugger
		// console.log(options.gId)
		if(options.gId!=''&&options.gId!=undefined&&options.gId!=null){
			this.setData({
				uid: userInfo.uid||wx.getStorageSync('userInfo').uid,
        // uid: 'da3bdf61cc1a409cafe573ea389c0e09', //测试uid
				 id:options.gId
			})
		}else if(options.id!=''&&options.id!=undefined&&options.id!=null){
			this.setData({
				uid: userInfo.uid,
				id:options.id
			})
		}
    this.getLocation()
		// this.addressList();
	},
  // 获取位置
  getLocation() {
    
    var _this = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // console.log(res);
        var userInfo = wx.getStorageSync('userInfo')
        if (res.errMsg == 'getLocation:ok') {
          userInfo['latitude'] = res.latitude
          userInfo['longitude'] = res.longitude
          wx.setStorageSync('userInfo', userInfo);
        }

      }
    })
  },
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		if(wx.getStorageSync('userInfo')!=''){
			this.setData({
				uid: wx.getStorageSync('userInfo').uid,
				// role: wx.getStorageSync('userInfo').role
			})
		}
		this.addressList();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},
	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		wx.showNavigationBarLoading() //在标题栏中显示加载
		this.setData({
			adressList: []
		})
		this.addressList()
		setTimeout(function() {
			wx.hideNavigationBarLoading() //完成停止加载
			wx.stopPullDownRefresh() //停止下拉刷新
		}, 1500);
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	// 地址列表
	addressList() {
		var source = {
			uid:this.data.uid||wx.getStorageSync('userInfo').uid,
		}
		api.post(addressList.list, source).then(res => {
			this.setData({
				adressList: res
			})
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	},
	// 设置为默认
	checkboxChange(e) {
		// console.log(e)
		if (e.detail.value[0]) {
			this.setData({
				adressId: e.detail.value[0]
			})
		}
		var Index = e.currentTarget.dataset.index
		var adressList=this.data.adressList
		// console.log(this.data.adressList[Index].isDefault,111)
		for(var i=0;i<adressList.length;i++){
			if(adressList[Index].isDefault==1){
				adressList[Index].isDefault=0;
				this.setData({
					adressList:adressList
				})
			}else if(adressList[Index].isDefault==0){
				//adressList[i].isDefault==0;
				adressList[Index].isDefault=1;
				this.setData({
					adressList:adressList
				})
			}
		}
		var source = {
			uid:wx.getStorageSync('userInfo').uid,
			id:this.data.adressId,
		}
		api.post(addressList.defaultList, source).then(res => {
			// console.log(res)
			this.addressList()			
		}).catch(err => {
			// console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})	
    //调用调转接口
    this.backToOrderDetail(e);
	},
	// 返回到上一页
	backToOrderDetail(e){
		// console.log(e)
		// console.log(this.data.id,'wedrew',e.currentTarget.dataset.adressid,'地址id')
		
		var pages = getCurrentPages(); // 获取页面栈
		var currPage = pages[pages.length - 1]; // 当前页面
		var prevPage = pages[pages.length - 2]; // 上一个页面
		// console.log(prevPage.data,'prevPage.data.addressInfor')
		prevPage.setData({
			adress:e.currentTarget.dataset.adressid
		})
		wx.navigateBack({
		  delta:1
		})
		// if(this.data.id!=''&&this.data.id != null && this.data.id != undefined){
		// 	wx.navigateBack({
		// 	  delta:1
		// 	})
		// 	// wx.redirectTo({
		// 	// 	url: '../good-payment/good-payment?id='+this.data.id+'&type=1'+'&adressid='+e.currentTarget.dataset.adressid
		// 	// })
		// }else{
		// 	wx.redirectTo({
		// 		url: '../good-payment/good-payment'
		// 	})
		// }	
	},
	adressDetail(e) {
		// console.log(e)
		wx.navigateTo({
			url: '../edit-adress/edit-adress?tid='+e.currentTarget.dataset.id
		})
	},
	// 添加新地址
	addNewAddress() {
		wx.navigateTo({
			url: '../add-receiveingAdress/add-receiveingAdress?gId='+this.data.id
		})
	},
	// 删除收货地址
	deleteAdress(e){
		var source = {
			uid:this.data.uid,
			tid:e.currentTarget.dataset.tid,
		}
		api.post(addressList.deleteAdress, source).then(res => {
			if(res>0){
				this.addressList()
			}
		}).catch(err => {
			console.log(err)
			wx.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	}
})
